import {
  ChangeDetectionStrategy,
  Component,
  OnInit,
  ViewEncapsulation,
  Input,
  TemplateRef,
  Optional,
  ChangeDetectorRef,
  OnDestroy,
} from '@angular/core';
import { Subscription } from 'rxjs';
import { HelpTooltipService } from '../core/services/help-tooltip.service';

@Component({
  selector: 'lv-help-tooltip',
  templateUrl: './help-tooltip.component.html',
  styles: [],
  encapsulation: ViewEncapsulation.None,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class HelpTooltipComponent implements OnInit, OnDestroy {
  @Input() lvIcon: string;
  @Input() lvContent: string | TemplateRef<any>;
  private status$: Subscription;

  constructor(
    private cdr: ChangeDetectorRef,
    @Optional() private helpTooltipService: HelpTooltipService,
  ) {}

  ngOnDestroy(): void {
    this.status$?.unsubscribe();
  }

  ngOnInit(): void {
    this.status$ = this.helpTooltipService?.status.subscribe(({ lvIcon, lvContent }): void => {
      this.lvIcon = lvIcon;
      this.lvContent = lvContent;
      this.cdr.markForCheck();
    });
  }
}
